package com.okilo.online.app.pet

import com.okilo.online.app.OkiloApplication
import com.okilo.online.app.common.IOkiloComView
import com.okilo.online.app.common.OkiloComPet
import com.okilo.online.app.configs.OkiloConfigs
import com.okilo.online.app.configs.OkiloConfigs.addParams
import com.okilo.online.app.event.OkiloEventData
import com.okilo.online.app.request.IHttpCallback
import com.okilo.online.app.pet.view.IOkiloConfigs
import com.okilo.online.app.tools.helper.OkiloConfigCustomerHp
import com.okilo.online.app.tools.helper.OkiloConfigProcessHp
import org.json.JSONObject

/**
 * 客服配置、流程配置
 */
class OkiloConfigsPet(private val iView: IOkiloComView?): OkiloComPet(iView) {

    /**
     * 获取客服配置
     * /v1/getAppShowInfo
     */
    fun gOkiloConfigCustomerRequest(callback: IOkiloConfigs.IOkiloConfigCustomer){
        val httpUrl = OkiloConfigs.gOkiloConfigCustomerUrl()
        val params = JSONObject().apply {
            put("path", OkiloConfigs.gOkiloRequestPath("APP_INFO"))
            put("product", OkiloConfigs.OKILO_APPNAME)
        }
        doPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                obj?.let {
                    //保存所有数据
//                    val customize = obj.optJSONObject("customize")
//                    val customData = customize.optJSONObject("customData")
//                    val testVersion = JSONObject()
//                    testVersion.put("isMustUpdate",true)
//                    testVersion.put("versionCode","2")
//                    testVersion.put("versionName","2.0.0")
//                    testVersion.put("versionRemark","1.A brand new UI design.$2.Update more secure data processing.$3.Resolve known bugs, optimize code, and enhance user experience.")
//                    customData.put("release",testVersion)

                    OkiloConfigCustomerHp.sOkiloConfigCustomerData(it.toString())
                    callback.sOkiloConfigCustomerCallback()
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                callback.hideOkiloLoading()
            }
        })
    }

    /**
     * 获取流程配置
     * /v1/userProcess
     */
    fun gOkiloConfigProcessRequest(callback: IOkiloConfigs.IOkiloConfigProcess){
        val httpUrl = OkiloConfigs.gOkiloConfigCustomerUrl()
        val params = JSONObject().addParams().apply {
            put("path", OkiloConfigs.gOkiloRequestPath("PROCESS"))
        }
        doPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
//                    TODO: 测试添加 Step 类型
//                    val allStepTitles = obj.optJSONArray("allStepTitles")
//                    allStepTitles.put("Account Info")
//                    allStepTitles.put("Ocr Info")
//                    allStepTitles.put("Face recognition")
//                    val allSteps = obj.optJSONArray("allSteps")
//                    allSteps.put("account_info")
//                    allSteps.put("ocr_info")
//                    allSteps.put("sefie_info")
//                    val steps = obj.optJSONArray("steps")
//                    steps.put("account_info")
//                    steps.put("sefie_info")
//                    steps.put("ocr_info")
//                    val index = Random.nextInt(3)
//                    when(index){
//                        0 -> {
//                            steps.put("ocr_info")
//                        }
//                        1 -> {
//                            steps.put("sefie_info")
//                        }
//                        else -> {
//                            steps.put("account_info")
//                        }
//                    }
                    //保存用户流程数据
                    OkiloConfigProcessHp.sOkiloConfigProcessData(obj)

                    //通知刷新流程完成
                    OkiloApplication.app.publisher.publish(OkiloEventData(OkiloEventData.TYPE.OKILO_PROCESS_REFRESHED))

                    callback.sOkiloConfigProcessCallback()
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 获取配分配置
     */
    fun gOkiloConfigScoreRequest(callback: IOkiloConfigs.IOkiloConfigScore){
        val httpUrl = OkiloConfigs.gOkiloRequestUrl()
        val params = JSONObject().addParams().apply {
            put("path", OkiloConfigs.gOkiloRequestPath("COMMENT_STAR"))
        }
        doPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    //设置true 需要增加显示次数
                    obj.put("isNeedAdd",true)
                    callback.sOkiloConfigScoreCallback(obj)
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
            }
        })
    }
}